package cn.jolyoulu.cmnservice.entity.po;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author LuZhouJin
 * @Date 2023/3/15
 */
@Data
@Accessors(chain = true)
public class HoldUser{

    /**用户信息*/
    private SysUser sysUser;

    /**角色信息*/
    private List<Role> roles;

    /**权限信息*/
    private Set<String> permissions;

    /**
     * 获取用户id
     * @return
     */
    public String getUserId() {
        if (Objects.isNull(sysUser)){
            return null;
        }
        return sysUser.getId();
    }

    /**
     * 获取用户角色Id集合
     * @return
     */
    public Set<String> getUserRoleIds() {
        if (Objects.isNull(roles)){
            return null;
        }
        return roles.stream().map(Role::getId).collect(Collectors.toSet());
    }

    /**
     * 获取创建者
     * @return 当前登录的userId(未登录返回System)
     */
    public String getCreateBy() {
        if (Objects.isNull(sysUser)){
            return "System";
        }
        return getUserId();
    }

    /**
     * 获取更新者
     * @return 当前登录的userId(未登录返回System)
     */
    public String getUpdateBy() {
        if (Objects.isNull(sysUser)){
            return "System";
        }
        return getUserId();
    }

    /**
     * 是否拥有权限
     */
    public boolean hasPermission(String permission) {
        if (Objects.isNull(permissions)){
            return false;
        }
        return permissions.contains(permission);
    }

    @Data
    public static class Role{
        private String id;
        private String roleType;

        public Role(SysRole sysRole) {
            this.id = sysRole.getId();
            this.roleType = sysRole.getRoleType();
        }
    }
}
